﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microhard.Framework.MVCHelper
{
    public class ScriptGridDeleteButton : BaseButton
    {
        private string _actionName;
        private string _controllerName;

        public ScriptGridDeleteButton()
        {
            this._assetKey = SysConst.OPERATION_DELETE;
        }

        public override System.Web.Mvc.MvcHtmlString WriteHtml(System.Web.Mvc.HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            this._actionName = actionName;
            this._controllerName = controllerName;

            return base.CreateButton(SysConst.OPERATION_DELETE, helper, this.ID, linkText, false, null, null, null);
        }

        public override string WriteJQueryReadyInnerScript()
        {
            StringBuilder script = new StringBuilder();
            script.Append("$('#" + this._id + "').click(function () {");
            script.Append(" var checkedIDs = $('." + SysConst.GRID_CKS_CSS + ":checked');");
            script.Append(" if (checkedIDs.length == 0) {");
            script.Append(" alert(\"请选择一条数据\");");
            script.Append(" return;");
            script.Append(" }");
            script.Append(" var parms = [];");
            script.Append(" $.each(checkedIDs, function (n, item) {");
            script.Append(" parms[n] = item.value;");
            script.Append(" });");
            script.Append(" var users = $.toJSON(parms);");
            script.Append(" if(confirm(\"您确定执行删除操作吗?\")){");
            script.Append(" $.ajax({");
            script.Append(" url: '" + this._controllerName + "/" + this._actionName + "',");
            script.Append(" type: 'POST',");
            script.Append(" dataType: 'json',");
            script.Append(" data: users,");
            script.Append(" contentType: 'application/json; charset=utf-8',");
            script.Append(" success: function (data) {");
            script.Append(" if(data != 'succeed'){alert(data);return;}");
            script.Append("var grid = $('#" + this._gridID + "').data('tGrid');");
            script.Append("grid.currentPage = 1;");
            script.Append("grid.ajaxRequest();");
            script.Append(";}});");
            script.Append("}");
            script.Append("});");

            return script.ToString();
             
        }
    }
}
